﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dal;
using Model;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
              
            //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            var user = new UserContext();

           ViewBag.User = user.User.FirstOrDefault().Name;
            //return Json(user.User.FirstOrDefault(), JsonRequestBehavior.AllowGet);
            //user.User.Add(new User()
            //    {
            //        Name = "13",
            //        Pwd = "13"
            //    })
            //;
            //user.SaveChanges();
            return View();
        }


        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        public  ActionResult Getuser()
        {
            var user = new UserContext();
            return Json(user.User, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// View  Login
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// Action  Login
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public  ActionResult Login(TestModels model)
        {
             
                var user = new UserContext();
                var result = user.User.Any(m => m.Name == model.username && m.Pwd == model.password);
                return Json(new {Result = result});

        }


        public ActionResult Remove(int id)
        {

            int result = 0;
            using (var user = new UserContext())
            {
                var con = user.User.FirstOrDefault(m => m.Id == id);
                user.Set<User>().Attach(con);
                user.Entry(con).State = EntityState.Deleted;

                result = user.SaveChanges();

            }

            return Json(new { Result = result > 0 });
        }

        /// <summary>
        /// User
        /// </summary>
        /// <returns></returns>
        public ActionResult UserList()
        {
            return View("User");
        }

    }
}
